import request from '@/utils/request'
import { resDataType, searchOptionsType, loginType, registerType } from '@/types'

export const api_createQuestion = () => {
  return request({
    url: '/api/question',
    method: 'post',
  })
}

export const api_getQuestionById = (id: string): Promise<resDataType> => {
  return request({
    url: `/api/question/${id}`,
    method: 'get',
  })
}

export const api_getQuestionList = (params: Partial<searchOptionsType>): Promise<resDataType> => {
  return request({
    url: '/api/question',
    method: 'get',
    params,
  })
}

export const api_updateQuestionById = (id: string, data: resDataType): Promise<resDataType> => {
  return request({
    url: `/api/question/${id}`,
    method: 'patch',
    data,
  })
}

export const api_copyQuestionById = (id: string): Promise<resDataType> => {
  return request({
    url: `/api/question/duplicate/${id}`,
    method: 'post',
  })
}

export const api_deleteQuestionBatch = (ids: string[]): Promise<resDataType> => {
  return request({
    url: '/api/question',
    method: 'delete',
    data: {
      ids,
    },
  })
}

export const api_getUserInfo = (): Promise<resDataType> => {
  return request({
    url: '/api/user/info',
    method: 'get',
  })
}

export const api_login = (data: loginType): Promise<resDataType> => {
  return request({
    url: '/api/user/login',
    method: 'post',
    data,
  })
}

export const api_register = (data: registerType): Promise<resDataType> => {
  return request({
    url: '/api/user/register',
    method: 'post',
    data,
  })
}

export const api_getAnswerList = (id: string, params: any): Promise<resDataType> => {
  return request({
    url: `/api/state/${id}`,
    method: 'get',
    params,
  })
}

export const api_getStateData = (questionId: string, componentId: string): Promise<resDataType> => {
  return request({
    url: `/api/state/${questionId}/${componentId}`,
    method: 'get',
  })
}
